package juc.c_020;/*
   Talk is cheap. Show me the code.
 */

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @Description
 * @Date 2021/6/8
 * @Author liudl
 */
public class T04_ReentrantLock {
    private static ReentrantLock lock = new ReentrantLock();
    private void m1() {
        try {
            lock.lockInterruptibly();
            try {
                TimeUnit.SECONDS.sleep(Integer.MAX_VALUE);
                System.out.println("m1...........");
            } catch (InterruptedException e) {
                System.out.println("Interrupted");;
            }
            finally {
                lock.unlock();
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    private void m2(){
        lock.lock();
        try {
            System.out.println("m2...........");
        } finally {
            lock.unlock();
        }
    }

    public static void main(String[] args) throws InterruptedException {
        T04_ReentrantLock t = new T04_ReentrantLock();
        Thread t1 = new Thread(t::m1);
        t1.start();
        new Thread(t::m2).start();
        TimeUnit.SECONDS.sleep(3);
        t1.interrupt();
    }
}
